package ihis.core.fetcher;

import ihis.core.db.IHISDatabase;
import ihis.model.IHISConstants;
import ihis.model.Performance;
import ihis.model.PredictedCurve;
import ihis.model.Schedule;
import ihis.model.Task;
import ihis.model.TaskFilter;
import ihis.tms.client.IHISTMSClient;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

public class Fetcher {
	private IHISTMSClient client = new IHISTMSClient();
	
	public static void main(String[] args) {
		if(args.length != 3 && args.length != 4 ){
			System.out.println("Usage:");
			System.out.println("java Fetcher hostname port (all)");
			System.out.println("ex) java Fetcher localhost 7403");
			return;
		}
		
		String hostName = args[1];
		long millis = System.currentTimeMillis();
		int port = Integer.parseInt(args[2]);
		Date date = new Date();			

		if(args.length == 3)
		{
			date.setTime(millis);
			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.setTime(date);
			if(	cal.get(Calendar.HOUR_OF_DAY) == 0)
			{
				cal.set(Calendar.HOUR_OF_DAY, -1);
			}

			new Fetcher().doFetch(hostName, port, date);
		}
		else if(args.length == 4 && args[3].equals("all"))
		{
			TaskFilter filter = new TaskFilter();
			IHISDatabase.deletePerformance(filter);
			IHISDatabase.deletePredictedCurve(filter);
			IHISDatabase.deleteRisk(filter);
			IHISDatabase.deleteSchedule(filter);
			
			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.set(2011, 3, 3, 0, 0, 0);
			while(true)
			{
				date = cal.getTime();
				new Fetcher().doFetch(hostName, port, date);
				if(millis < date.getTime())
					break;
				cal.add(Calendar.DATE, 7);
			}
			
		}
	}
	
	public void doFetch(String hostName, int serverPort, Date date){
		client.setHostName(hostName);
		client.setServerPort(serverPort);
		
		fetchRisk();
		
		TaskFilter filter = new TaskFilter();
		
		filter.setStartDate(date);
		filter.setEndDate(date);
		filter.setOwnerType(IHISConstants.OWNERTYPE_PERSON);
		filter.setPeriodType(IHISConstants.TIME_UNIT_WEEKLY);
		fetchSchedule(filter);
		fetchPerformance(filter);
		fetchPredictedCurve(filter);
		
		filter.setOwnerType(IHISConstants.OWNERTYPE_PERSON);
		filter.setPeriodType(IHISConstants.TIME_UNIT_MONTHLY);
		fetchSchedule(filter);
		fetchPerformance(filter);
		fetchPredictedCurve(filter);
		
		filter.setOwnerType(IHISConstants.OWNERTYPE_TEAM);
		filter.setPeriodType(IHISConstants.TIME_UNIT_WEEKLY);
		fetchSchedule(filter);
		fetchPerformance(filter);
		fetchPredictedCurve(filter);
		
		filter.setOwnerType(IHISConstants.OWNERTYPE_TEAM);
		filter.setPeriodType(IHISConstants.TIME_UNIT_MONTHLY);
		fetchSchedule(filter);
		fetchPerformance(filter);
		fetchPredictedCurve(filter);
		
		filter.setOwnerType(IHISConstants.OWNERTYPE_PERSON);
		filter.setPeriodType(IHISConstants.TIME_UNIT_YEARLY);
		fetchPredictedCurve(filter);
		fetchPerformance(filter);
		
		filter.setOwnerType(IHISConstants.OWNERTYPE_TEAM);
		filter.setPeriodType(IHISConstants.TIME_UNIT_YEARLY);
		fetchPredictedCurve(filter);
		fetchPerformance(filter);

		filter.setOwnerType(IHISConstants.OWNERTYPE_PROJECT);
		filter.setPeriodType(IHISConstants.TIME_UNIT_WEEKLY);
		fetchPredictedCurve(filter);

		filter.setPeriodType(IHISConstants.TIME_UNIT_MONTHLY);
		fetchPredictedCurve(filter);

		filter.setPeriodType(IHISConstants.TIME_UNIT_YEARLY);
		fetchPredictedCurve(filter);

	}
	
	public void fetchPredictedCurve(TaskFilter filter){
		Vector<PredictedCurve> list = client.getPredictedCurveList(filter);
		if(list == null || list.size() == 0){
			return;
		}
		
		for(PredictedCurve curve: list){
			IHISDatabase.insertPredictedCurve(curve);
		}
	}
	
	public void fetchPerformance(TaskFilter filter){
		Vector<Performance> list = client.getPerformanceList(filter);
		if(list == null || list.size() == 0){
			return;
		}
		
		for(Performance performance : list){
			IHISDatabase.insertPerformance(performance);
		}
	}
	
	public void fetchSchedule(TaskFilter filter){
		Vector<Schedule> list = client.getScheduleList(filter);
		if(list == null || list.size() == 0){
			return;
		}
		
		for(Schedule schedule : list){
			IHISDatabase.insertSchedule(schedule);
		}
	}
	
	public void fetchRisk(){
		TaskFilter filter = new TaskFilter();
		IHISDatabase.deleteRisk(filter);
		
		Vector<Task> list = client.getRiskList();
		if(list != null && list.size() > 0){
			for(Task task : list){
				IHISDatabase.insertRisk(task);
			}
		}
		filter.setTaskType(IHISConstants.TASK_LIST_DELAYED);
		filter.setStartDate(new Date(System.currentTimeMillis()));
		list = client.getTaskList(filter);
		if(list != null && list.size() > 0){
			for(Task task : list){
				IHISDatabase.insertRisk(task);
			}
		}
		
	}
}
